﻿using ReactiveUI;
using System.Collections.Generic;
using System.Reactive;
using System.Runtime.CompilerServices;
using System;
using AopCommon;

namespace AopAvalonia.ViewModels
{
    public class ViewModelBase : ReactiveObject
    {
        #region Aop的方式创建Command,用来记录用户操作

        public ReactiveCommand<Unit, Unit> CreateOrGet(Action execute, [CallerMemberName] string callerMemberName = null)
        {

            ReactiveCommand<Unit, Unit> command = ReactiveCommand.Create(() =>
            {
                MessageBus.Current.SendMessage<PromptMsg>($"进入函数{callerMemberName} 时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                execute?.Invoke();
                MessageBus.Current.SendMessage<PromptMsg>($"退出函数{callerMemberName}  时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
            });
            ;
            return command;
        }

        public ReactiveCommand<Unit, TResult> CreateOrGet<TResult>(Func<TResult> execute, [CallerMemberName] string callerMemberName = null)
        {

            ReactiveCommand<Unit, TResult> command = ReactiveCommand.Create(() =>
            {
                MessageBus.Current.SendMessage<PromptMsg>   ($"进入函数{callerMemberName} 时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                TResult result1 = execute.Invoke();
                MessageBus.Current.SendMessage<PromptMsg>($"退出函数{callerMemberName}  时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                return result1;
            });
            return command;
        }

        public ReactiveCommand<TParam, Unit> CreateOrGet<TParam>(Action<TParam> execute,
            [CallerMemberName] string callerMemberName = null)
        {

            ReactiveCommand<TParam, Unit> command = ReactiveCommand.Create((TParam param) =>
            {
                MessageBus.Current.SendMessage<PromptMsg>($"进入函数{callerMemberName} 时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                execute?.Invoke(param);
                MessageBus.Current.SendMessage<PromptMsg>($"退出函数{callerMemberName}  时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
            });
            return command;
        }

        public ReactiveCommand<TParam, TResult> CreateOrGet<TParam, TResult>(Func<TParam, TResult> execute, [CallerMemberName] string callerMemberName = null)
        {
            ReactiveCommand<TParam, TResult> command = ReactiveCommand.Create<TParam, TResult>((TParam param) =>
            {
                MessageBus.Current.SendMessage<PromptMsg>($"进入函数{callerMemberName} 时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                TResult result = execute.Invoke(param);
                MessageBus.Current.SendMessage<PromptMsg>($"退出函数{callerMemberName}  时间${DateTime.Now.ToString("yyyyMMdd HH: mm:ss: fff")}");
                return result;
            });
            return command;
        }
        #endregion
    }
}